package com.example.controller;

import com.example.dto.User;
import com.example.service.UserService;
import com.example.util.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

@Controller
@RequestMapping("mydb")
public class TestDBController {
    private static final Logger logger = LoggerFactory.getLogger(TestDBController.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private UserService userService;

    @Resource(name="redisTestTemplate")
    private RedisTemplate redisTemplate;

    @GetMapping("/getRedis")
    @ResponseBody
    public String getRedis() {
        RedisUtil redisUtil = new RedisUtil();
        redisUtil.setRedisTemplate(redisTemplate);
        String msg = redisUtil.get("aaa").toString();
//        redisUtil.set("aaa", "11111111");
        return msg;
    }

    @GetMapping("/get")
    @ResponseBody
    public List<User> getList(HttpServletRequest request, HttpServletResponse response) {
        logger.info("--------------comm here----------");
//        String sql = "select * from t_users";
//        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        User user = new User();
        List<User> list = userService.getUserList(user);
        if (list == null) {
            logger.info("------->暂无数据");
            return null;
        }
        for (User userTmp : list) {
            logger.info("---------用户信息:{}", userTmp);
        }
        request.getSession().setAttribute("list", list);
//        Cookie[] cookies = request.getCookies();
//        for(Cookie cookie:cookies){
//            logger.info ("name = {}", cookie.getName());
//            logger.info("value = {}", cookie.getValue());
//        }
        return list;
    }

    @RequestMapping("getUser")
    public void getUser(HttpServletRequest request, HttpServletResponse response) {
        String querStr = "select * from t_users";
        User user = new User();

        List<Map<String, Object>> list = jdbcTemplate.queryForList(querStr);
        for (Map<String, Object> map : list) {
            user.setUserName(map.get("username").toString());
        }
        request.getSession().setAttribute("user", user);
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            logger.info("name = {}, value = {}", cookie.getName(), cookie.getValue());
        }
        logger.info("--------->name = {}", user.getUserName());
    }


    @RequestMapping("setssion")
    public void setSession(HttpServletRequest request, HttpServletResponse response) {
        String jessionid = request.getParameter("JSESSIONID");
        Cookie cookie = new Cookie("JSESSIONID", jessionid);
        response.addCookie(cookie);
    }

    @RequestMapping("msg")
    public String getHtml(HttpServletRequest request, HttpServletResponse response) {
        return "user";
    }

    @RequestMapping("/login")
    public String login(Map<String, Object> map) {
        map.put("name", "Joe");
        map.put("sex", 1);    //sex:性别，1：男；0：女；

        // 模拟数据
        List<Map<String, Object>> friends = new ArrayList<Map<String, Object>>();
        Map<String, Object> friend = new HashMap<String, Object>();
        friend.put("name", "xbq");
        friend.put("age", 22);
        friends.add(friend);
        friend = new HashMap<String, Object>();
        friend.put("name", "July");
        friend.put("age", 18);
        friends.add(friend);
        map.put("friends", friends);
        return "login";
    }
}
